//
//  RegisterViewController.m
//  IceBreaker
//
//  Created by Chiao Wolfire on 12-10-3.
//  Copyright (c) 2012年 NDS. All rights reserved.
//

#import "RegisterViewController.h"
#import "TabsViewController.h"
#import "NSData+Base64.h"
#import "User.h"
#import "UserDetailViewController.h"
#import "PublicUtils.h"

@implementation RegisterViewController

@synthesize mailLabel, pwdLabel/*, confirmLabel*/;
@synthesize mailTxt, pwdTxt/*, confirmPwd*/;
@synthesize registerBtn, cancelBtn;

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedStringFromTable(@"Register", @"Localizable", @"Register");
    }
    return self;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [mailLabel setText: NSLocalizedStringFromTable(@"Mail box", @"Localizable", @"Mail box")];        
    [pwdLabel setText: NSLocalizedStringFromTable(@"Password", @"Localizable", @"Password")];
    // [confirmLabel setText: NSLocalizedStringFromTable(@"Comfirm password", @"Localizable", @"Comfirm password")];
    [mailTxt setPlaceholder: NSLocalizedStringFromTable(@"Input mail", @"Localizable", @"Please input your mail box")];
    [mailTxt setDelegate: self];
    [pwdTxt setPlaceholder: NSLocalizedStringFromTable(@"Input pwd", @"Localizable", @"Please input your password")];
    [pwdTxt setDelegate: self];
    pwdTxt.secureTextEntry = YES;
    // [confirmPwd setPlaceholder: NSLocalizedStringFromTable(@"Comfirm pwd", @"Localizable", @"Please comfirm your password")];
    // [confirmPwd setDelegate: self];
    // confirmPwd.secureTextEntry = YES;
    [registerBtn setTitle: NSLocalizedStringFromTable(@"Register", @"Localizable", @"Register") 
                 forState: UIControlContentHorizontalAlignmentCenter];
    [cancelBtn setTitle: NSLocalizedStringFromTable(@"Cancel", @"Localizable", @"Cancel") 
               forState: UIControlContentHorizontalAlignmentCenter];
}

-(void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;mailLabel, *pwdLabel/*, *confirmLabel*/;
    mailTxt = nil;
    pwdTxt = nil;
    registerBtn = nil;
    cancelBtn = nil;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - instance methods
-(IBAction) regBtnClicked:(id)sender {
    NSString *myStr = @"Base64 tseting, I'm in a bad situation now...";
    NSLog(@"The original string for test is [%@]", myStr);
    NSData *myData = [myStr dataUsingEncoding: NSUTF8StringEncoding];
    NSLog(@"OK, let's encode it first:");
    NSString *encoded = [NSData encodeBase64: myData];
    NSLog(@"Check the encoded string: [%@]. It's time to decode it", encoded);
    NSData *decode = [NSData decodeBase64: encoded];
    NSLog(@"Check the decoded data: [%@], parse it a string", decode);
    NSString *aResult = [[NSString alloc] initWithData: decode 
                                              encoding: NSUTF8StringEncoding];
    NSLog(@"The final decoded string is [%@]", aResult);
    if ([myStr isEqualToString: aResult]) {
        NSLog(@"Great, Base64 works well!");
    } else {
        NSLog(@"Oops, something wrong to Base64....");
    }
    
    if (mailTxt.text == nil || [mailTxt.text isEqualToString: @""]) {
        [PublicUtils alertMsgWithTitle: NSLocalizedStringFromTable(@"Alert tips", @"Localizable", @"Tips") 
                               message: @"注册邮箱不能为空！" 
                     cancelButtonTitle: NSLocalizedStringFromTable(@"Confirm text", @"Localizable", @"OK")];

        [mailTxt becomeFirstResponder];
        return;
    }
    if (pwdTxt.text == nil || [pwdTxt.text isEqualToString: @""]) {
        [PublicUtils alertMsgWithTitle: NSLocalizedStringFromTable(@"Alert tips", @"Localizable", @"Tips") 
                               message: @"密码不能为空！" 
                     cancelButtonTitle: NSLocalizedStringFromTable(@"Confirm text", @"Localizable", @"OK")];
        
        [pwdTxt becomeFirstResponder];
        return;
    }    
    
    User *user = [[User alloc] initWithMailBox: mailTxt.text 
                                      password: pwdTxt.text];
    UserDetailViewController *userDetail = [[UserDetailViewController alloc] initWithNibName: @"UserDetailViewController" 
                                                                                      bundle: nil];
    userDetail.user = user;
    [self presentModalViewController: userDetail 
                            animated: YES];
}

-(IBAction) cancel:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"确认放弃" 
                                                    message: @"您确定要放弃注册？" 
                                                   delegate: self 
                                          cancelButtonTitle: @"放弃" 
                                          otherButtonTitles: @"继续", nil];
    [alert show];
    [alert release];
}

-(IBAction)closeViewEdit:(id)sender {
    [mailTxt resignFirstResponder];
    [pwdTxt resignFirstResponder];
    // [confirmPwd resignFirstResponder];
    self.view.center = CGPointMake(160, 250);
}

#pragma mark - method for UIAlertViewDelegate
-(void) alertView:(UIAlertView *)alertView 
clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            [mailTxt setText: @""];
            [mailTxt setPlaceholder: NSLocalizedStringFromTable(@"Input mail", @"Localizable", @"Please input your mail box")];
            [pwdTxt setText: @""];
            [pwdTxt setPlaceholder: NSLocalizedStringFromTable(@"Input pwd", @"Localizable", @"Please input your password")];
            // [confirmPwd setText: @""];
            // [confirmPwd setPlaceholder: NSLocalizedStringFromTable(@"Comfirm pwd", @"Localizable", @"Please comfirm your password")];
            // [self.navigationController popViewControllerAnimated: YES];
            
            [self dismissModalViewControllerAnimated: YES];
            break;
        case 1:
            break;
        default:
            break;
    }
}

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
    // the original coordination of the screen is (160, 250)
    // up it to make the text feild visible
    switch ([textField tag]) {
        case 0:
            self.view.center = CGPointMake(160, 240);
            break;
        case 1:
            self.view.center = CGPointMake(160, 210);
            break;
        case 2:
            self.view.center = CGPointMake(160, 180);
            break;
        default:
            break;
    }

    return YES;
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField {
    // [confirmPwd resignFirstResponder];
    self.view.center = CGPointMake(160, 250);
    switch ([textField tag]) {
        case 0:
            [mailTxt resignFirstResponder];
            break;
        case 1:
            [pwdTxt resignFirstResponder];
            [self regBtnClicked: registerBtn];
            break;
        default:
            break;
    }
    return YES;
}

@end
